﻿
Partial Class Admin_UserAdmin
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim _list As MembershipUserCollection = Membership.GetAllUsers
            rptrList.DataSource = _list
            rptrList.DataBind()
        End If
    End Sub

    Protected Sub rptrList_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptrList.ItemDataBound
        If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
            Dim lblName As Label = CType(e.Item.FindControl("lblName"), Label)
            Dim lblEmail As Label = CType(e.Item.FindControl("lblEmail"), Label)
            Dim chkAdmin As CheckBox = CType(e.Item.FindControl("chkAdmin"), CheckBox)
            Dim chkApproved As CheckBox = CType(e.Item.FindControl("chkApproved"), CheckBox)
            Dim lblSince As Label = CType(e.Item.FindControl("lblSince"), Label)
            Dim u As MembershipUser = CType(e.Item.DataItem, MembershipUser)
            lblName.Text = "<a href=""" & Utils.RelativeWebRoot & "Members/" & u.UserName & """ title=""" & u.UserName & """>" & u.UserName & "</a>"
            chkApproved.Checked = u.IsApproved
            chkApproved.ToolTip = u.UserName
            Dim _roles() As String = Roles.GetRolesForUser(u.UserName)
            chkAdmin.Checked = _roles.Contains("Administrator")
            chkAdmin.ToolTip = u.UserName
            lblEmail.Text = u.Email
            lblSince.Text = u.CreationDate.ToShortDateString
        End If
    End Sub

    Protected Sub chk_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim chk As CheckBox = CType(sender, System.Web.UI.WebControls.CheckBox)
        Select Case chk.ID
            Case "chkApproved"
                Dim usr As MembershipUser = Membership.GetUser(chk.ToolTip)
                usr.IsApproved = chk.Checked
                Membership.UpdateUser(usr)
            Case "chkAdmin"
                If Roles.IsUserInRole(chk.ToolTip, "Administrator") Then
                    Roles.RemoveUserFromRole(chk.ToolTip, "Administrator")
                Else
                    Roles.AddUserToRole(chk.ToolTip, "Administrator")
                End If
            Case Else

        End Select
    End Sub
End Class
